package com.tang.service;

import com.tang.entity.Topic;
import com.tang.pojo.AnswerQuestion;
import com.tang.pojo.ChoiceQuestion;
import com.tang.util.TopicRandomUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

public class QuestionBank implements Cloneable{

    @Getter
    @Setter
    private String candidate; // 考生

    @Getter
    @Setter
    private String number; // 考号

    private ArrayList<ChoiceQuestion> choiceQuestionList = new ArrayList<>();
    private ArrayList<AnswerQuestion> answerQuestionList = new ArrayList<>();

    public QuestionBank append(ChoiceQuestion choiceQuestion) {
        choiceQuestionList.add(choiceQuestion);
        return this;
    }

    public QuestionBank append(AnswerQuestion answerQuestion) {
        answerQuestionList.add(answerQuestion);
        return this;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        QuestionBank questionBank = (QuestionBank) super.clone();
        questionBank.choiceQuestionList = (ArrayList<ChoiceQuestion>) choiceQuestionList.clone();
        questionBank.answerQuestionList = (ArrayList<AnswerQuestion>) answerQuestionList.clone();

        Collections.shuffle(questionBank.choiceQuestionList);
        Collections.shuffle(questionBank.answerQuestionList);

        for (ChoiceQuestion choiceQuestion : questionBank.choiceQuestionList) {
            Topic random = TopicRandomUtil.random(choiceQuestion.getOption(), choiceQuestion.getKey());
            choiceQuestion.setOption(random.getOption());
            choiceQuestion.setKey(random.getKey());
        }
        return questionBank;
    }

    @Override
    public String toString() {
        StringBuilder detail = new StringBuilder("考生：");
        detail.append(candidate);
        detail.append("\r\n考号：");
        detail.append(number);
        detail.append("\r\n------------------------------------------------------\r\n");
        detail.append("一、选择题\r\n\n");
        int size = choiceQuestionList.size();
        for (int idx = 0; idx < size; idx++) {
            detail.append("第").append(idx + 1).append("题").append(choiceQuestionList.get(idx).getName())
                    .append("\r\n");
            Map<String, String> option = choiceQuestionList.get(idx).getOption();
            for (Map.Entry<String, String> entry : option.entrySet()) {
                detail.append(entry.getKey()).append("：").append(entry.getValue()).append("\r\n");
            }
            detail.append("答案：").append(choiceQuestionList.get(idx).getKey()).append("\r\n\n");
        }

        detail.append("二、问答题\r\n\n");

        for (int i = 0; i < answerQuestionList.size(); i++) {
            detail.append("第").append(i + 1).append("题：").append(answerQuestionList.get(i).getName()).append("\r\n");
            detail.append("答案：").append(answerQuestionList.get(i).getKey()).append("\r\n\n");
        }

        return detail.toString();
    }
}
